package cn.itcast.ft.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.itcast.ft.dto.*;
import cn.itcast.ft.entity.FTToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class WHCTitleController {
    private static List<FTToken> tokenList=new ArrayList<>();
    @RequestMapping("/hcTitleCreateMarket")
    public String hcTitleCreateMarket(@RequestParam(value = "num",defaultValue = "10000") Integer num,
                               @RequestParam(value = "salePrice",defaultValue = "29") String salePrice,
                               @RequestParam(value = "goodsId",defaultValue = "24") String goodsId) {
        if(CollectionUtils.isEmpty(tokenList)){
            addToken();
        }
        for(int i=0;i<num;i++){
            try {
                for(int z=0;z<tokenList.size();z++){
                    Proxy proxy =null;
                    confireMarketOrder(proxy,tokenList.get(z),goodsId,salePrice);
                    Thread.sleep(5000);
                }
            } catch (Exception e) {
                log.error("#####异常####" + e.getMessage());
            }
        }
        log.info("####FT执行完成#####");
        return null;
    }
    public void confireMarketOrder(Proxy proxy,FTToken token,String goodsId,String salePrice){
        try{
            createMarketOrder(proxy,token,goodsId,salePrice);
        }catch (Exception e){
            log.error("#####获取商品失败####"+e.getMessage()+"###"+token);
        }
    }
    public void createMarketOrder(Proxy proxy,FTToken token,String goodsId,String salePrice) throws InterruptedException {
        String response=null;
        WHZGoodsDTO goodsDTO=null;
        try{
            JSONObject jsonObject=new JSONObject();
            jsonObject.putOnce("product_id",goodsId);
            response = HttpRequest.get("https://duoliang.cc/api/v2/nft/composite/list?limit=10&page=1")
                    .header("Authori-Zation",token.getToken())
                    .body(jsonObject.toString())
                    .execute().body();
            log.info("#####确认订单结果####"+response);
            goodsDTO= JSONUtil.toBean(response,WHZGoodsDTO.class);
        }catch (Exception e){
            log.error("##获取商品数据异常###:"+e.getMessage()+"####返回数据：###"+response);
        }
        if(goodsDTO.getStatus()==200 && CollectionUtil.isNotEmpty(goodsDTO.getData().getList())){
            MailUtil.send("1027060531@qq.com", "收到订单", "您有一个订单:"+token.getRefreshToken()+"，请赶紧支付！", false);

        }
    }

    public void addToken(){
        FTToken token=new FTToken();
        token.setToken("Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJkdW9saWFuZy5jYyIsImF1ZCI6ImR1b2xpYW5nLmNjIiwiaWF0IjoxNzE2ODYzMDc0LCJuYmYiOjE3MTY4NjMwNzQsImV4cCI6MTcxOTQ1NTA3NCwianRpIjp7ImlkIjo2NTg2ODAsInR5cGUiOiJhcGkifX0.Hb8qvpMQn2nWJBh10P16QnjhBXxDeh8cCJQzuzqi4Rk");
        token.setRefreshToken("微信名字:万");
        tokenList.add(token);    }
}
